package com.tulingxueyuan.mall.modules.pms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tulingxueyuan.mall.common.api.CommonPage;
import com.tulingxueyuan.mall.common.api.CommonResult;
import com.tulingxueyuan.mall.modules.pms.model.PmsBrand;
import com.tulingxueyuan.mall.modules.pms.model.PmsProduct;
import com.tulingxueyuan.mall.modules.pms.model.dto.ProductConditionDTO;
import com.tulingxueyuan.mall.modules.pms.model.dto.ProductSaveParamsDTO;
import com.tulingxueyuan.mall.modules.pms.model.dto.ProductUpdateInitDTO;
import com.tulingxueyuan.mall.modules.pms.service.PmsProductService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 商品信息 前端控制器
 * </p>
 *
 * @author zhaofuhao
 * @since 2022-02-24
 */
@RestController
@RequestMapping("/product")
public class PmsProductController {
   @Autowired
   PmsProductService productService;

   /**
    *   url:'/product/list',
    *   method:'get',
    *   params:params
    *   {
    *     keyword: null,
    *     pageNum: 1,
    *     pageSize: 5,
    *     publishStatus: null,
    *     verifyStatus: null,
    *     productSn: null,
    *     productCategoryId: null,
    *     brandId: null
    *   };
    * @return
    */
   @ApiOperation("商品列表")
   @GetMapping (value = "/list")
   public CommonResult list(ProductConditionDTO condition){
      Page page = productService.list(condition);

      return CommonResult.success(CommonPage.restPage(page));

   }
   /**
    *    url:'/product/update/deleteStatus',
    *    method:'post',
    *    params:params
    */
   @PostMapping(value="/update/deleteStatus")
   public CommonResult delete(@RequestParam("ids") List<Long> ids){
      boolean result = productService.removeByIds(ids);
      if (result){
        return CommonResult.success(result);
      }else {
         return CommonResult.failed();
      }

   }
   /**
    * 更新是否新品状态
    * export function updateNewStatus(params) {
    *   return request({
    *     url:'/product/update/newStatus',
    *     method:'post',
    *     params:params
    *   })
    * }
    **/
   @PostMapping(value="/update/newStatus")
   public CommonResult updateNewStatus(@RequestParam("ids") List<Long> ids,
                              @RequestParam("newStatus") Integer newStatus){
    boolean result=productService.updateStatus(newStatus,ids,PmsProduct::getNewStatus);
    if (result){
     return CommonResult.success(result);
    }else {
      return CommonResult.failed();
    }
   }
   /**
    * 更新是否推荐
    * export function updateRecommendStatus(params) {
    *   return request({
    *     url:'/product/update/recommendStatus',
    *     method:'post',
    *     params:params
    *   })
    * }
    **/
   @PostMapping(value="/update/recommendStatus")
   public CommonResult updateRecommendStatus(@RequestParam("ids") List<Long> ids,
                              @RequestParam("recommendStatus") Integer recommendStatus){

      boolean result=productService.updateStatus(recommendStatus,ids,PmsProduct::getRecommandStatus);
       if (result){
           return CommonResult.success(result);
       }else {
           return CommonResult.failed();
       }

   }
    /**
     * 更新是否上架
    * export function updatePublishStatus(params) {
    *   return request({
    *     url:'/product/update/publishStatus',
    *     method:'post',
    *     params:params
    *   })
    * }
    */
    @PostMapping(value="/update/publishStatus")
    public CommonResult updatePublishStatus(@RequestParam("ids") List<Long> ids,
                               @RequestParam("publishStatus") Integer publishStatus){
       boolean result=productService.updateStatus(publishStatus,ids,PmsProduct::getPublishStatus);
        if (result){
            return CommonResult.success(result);
        }else {
            return CommonResult.failed();
        }
    }
    /**
     *  商品添加
     * url:'/product/create',
     *     method:'post',
     *     data:data    json
     */
    @PostMapping(value="/create")
    public CommonResult create(@RequestBody ProductSaveParamsDTO productSaveParamsDTO){
        boolean result= productService.create(productSaveParamsDTO);
        if(result){
            return CommonResult.success(result);
        }
        else {
            return CommonResult.failed();
        }
    }

    /**
     *   获取编辑状态下的商品信息
     *   url:'/product/updateInfo/'+id,
     *     method:'get',
     */
    @GetMapping("/updateInfo/{id}")
    public CommonResult getUpdateInfo(@PathVariable long id){
        ProductUpdateInitDTO updateInitDTO =  productService.getUpdateInfo(id);
        return CommonResult.success(updateInitDTO);
    }
    /**
     *    商品修改保存
     *    url:'/product/update/'+id,
     *     method:'post',
     *     data:data
     */
    @PostMapping(value="update/{id}")
    public CommonResult update(@RequestBody @Valid ProductSaveParamsDTO productSaveParamsDTO){
        boolean result= productService.update(productSaveParamsDTO);
        if(result){
            return CommonResult.success(result);
        }
        else {
            return CommonResult.failed();
        }
    }

}

